﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class CreateEditStocktakingLists : UserControl
    {
        Stocktaking NewStocktaking = new Stocktaking();
        bool edit = false;
        public CreateEditStocktakingLists()
        {
            Program.GetMainForm.Text = "TMS - Пописна листа";
            InitializeComponent();
        }

        public CreateEditStocktakingLists(DataGridViewRow parameters)
        {
            InitializeComponent();
            
            if (parameters.Cells.Count == 2)
            {
                Program.GetMainForm.Text = "TMS - Нова пописна листа";
                NewStocktaking.ID = Convert.ToInt32(parameters.Cells[0].Value);
                NewStocktaking.Date = Convert.ToDateTime(parameters.Cells[1].Value);
                lblStocktakingDate.Text = "Попис " +Convert.ToDateTime(parameters.Cells[1].Value).ToString("dd-MMM-yy");
            }
            else
            {
                Program.GetMainForm.Text = "TMS -Пописна листа: " + parameters.Cells[3].Value.ToString(); 
                NewStocktaking.StocktakingListID = Convert.ToInt32(parameters.Cells[0].Value);
                NewStocktaking.ID = Convert.ToInt32(parameters.Cells[1].Value);
                lblStocktakingDate.Text = "Попис " + parameters.Cells[2].Value.ToString();
                txtDescription.Text = parameters.Cells[3].Value.ToString();
                txtResponsible.Text = parameters.Cells[4].Value.ToString();
                edit = true;
            }

        }

        private void btnSaveEdit_Click(object sender, EventArgs e)
        {
            if (edit)
            {
                NewStocktaking.UpdateStocktakingList(txtDescription.Text, txtResponsible.Text);
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
            }
            else
            {
                NewStocktaking.AddStockingList(txtDescription.Text, txtResponsible.Text);
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            NewStocktaking.DeleteStockingList();
            this.Dispose();
            Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }
    }
}
